package com.wd.mylibrary_live.adapter

import android.content.Context
import android.media.MediaPlayer
import android.os.Handler
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.SeekBar
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import coil.load
import coil.transform.RoundedCornersTransformation
import com.wd.mylibrary_base.bean.MusicBean
import com.wd.mylibrary_live.R
import java.text.SimpleDateFormat

/**
 * 作者: 邢凌志
 * 时间: 2024/9/28
 * 描述: 播放列表适配器
 */
class MusicAdapter(
    private var context: Context,
    private var list:ArrayList<MusicBean.Result>,
    private var mediaPlayer: MediaPlayer,
):RecyclerView.Adapter<MusicAdapter.ViewHodel>() {

    var handler: Handler= Handler()

    class ViewHodel(viewHodel:View):RecyclerView.ViewHolder(viewHodel){
        var cover:ImageView = viewHodel.findViewById(R.id.cover)
        var musicName:TextView = viewHodel.findViewById(R.id.name)
        var singer:TextView = viewHodel.findViewById(R.id.singer)
        var seekBar:SeekBar = viewHodel.findViewById(R.id.sb)
        var duration:TextView = viewHodel.findViewById(R.id.duration)
        var statTime:TextView = viewHodel.findViewById(R.id.startTime)
        var startOrPause:ImageView = viewHodel.findViewById(R.id.startOrPause)
        var button:Button = viewHodel.findViewById(R.id.button)
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHodel {
        var view = LayoutInflater.from(this.context).inflate(R.layout.item_music, parent, false)
        return ViewHodel(view)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: ViewHodel, position: Int) {
        mediaPlayer = MediaPlayer()

        holder.cover.load(list[position].cover) {
            placeholder(R.mipmap.ic_launcher)
            error(R.mipmap.ic_launcher)
            transformations(RoundedCornersTransformation(10f))
        }
        holder.musicName.setText(list[position].name)
        holder.singer.setText(list[position].singer)
        holder.duration.setText(list[position].duration)
        holder.statTime.setText("00:00")

        holder.seekBar.visibility = View.GONE
        holder.startOrPause.visibility = View.GONE
        holder.statTime.visibility = View.GONE
        holder.duration.visibility = View.GONE
        holder.button.visibility = View.GONE



        list[position].isCheck=true
        holder.itemView.setOnClickListener {

            if (list[position].isCheck){
                list[position].isCheck=false
                startMusic(holder,position)
                mediaPlayer.start()
            }else{
                mediaPlayer.pause()
                holder.startOrPause.load(R.mipmap.pause)
                list[position].isCheck=true
            }

            holder.button.setOnClickListener {
                onMusic?.music(position,list)
                mediaPlayer.pause()
                mediaPlayer.reset()
            }
        }

    }

    fun startMusic(holder: ViewHodel,position: Int){
        holder.seekBar.visibility=View.VISIBLE
        holder.startOrPause.visibility = View.VISIBLE
        holder.statTime.visibility = View.VISIBLE
        holder.duration.visibility = View.VISIBLE
        holder.button.visibility = View.VISIBLE

        mediaPlayer.setDataSource(list[position].musicLinks)
        mediaPlayer.prepare()

        holder.startOrPause.setOnClickListener {
            if(mediaPlayer.isPlaying){
                holder.startOrPause.load(R.mipmap.pause)
                mediaPlayer.pause()

            }else{
                holder.startOrPause.load(R.mipmap.play)
                mediaPlayer.start()
            }
        }

        var format= SimpleDateFormat("mm:ss")
        val split=list[position].duration.split(":")
        handler.postDelayed(object :Runnable{
            override fun run() {
                var currentPosition= mediaPlayer.currentPosition
                holder.seekBar.max=(split[0].toInt()*60+split[1].toInt())*1000
                holder.seekBar.setProgress(currentPosition)
                var current=format.format(currentPosition)
                holder.statTime.setText(current)
                Handler().postDelayed(this,100)
            }
        },100)

        holder.seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
            override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
                if (p2){
                    mediaPlayer.seekTo(p1)
                }
            }

            override fun onStartTrackingTouch(p0: SeekBar?) {

            }

            override fun onStopTrackingTouch(p0: SeekBar?) {

            }

        })

    }


    interface OnClickListener{
        fun music(id:Int,list: ArrayList<MusicBean.Result>)
    }

    private var onMusic:OnClickListener?=null

    fun setOnClickListener(onMusic:OnClickListener?){
        this.onMusic=onMusic
    }

}

//for ((index,result)in list.withIndex()){
//    if (position!=index){
//        list[index].isCheck=false
//        notifyItemChanged(index)
//    }
//}
//override fun onBindViewHolder(holder: ViewHodel, position: Int) {
//        mediaPlayer = MediaPlayer()
//
//        holder.cover.load(list[position].cover) {
//            placeholder(R.mipmap.ic_launcher)
//            error(R.mipmap.ic_launcher)
//            transformations(RoundedCornersTransformation(10f))
//        }
//        holder.musicName.setText(list[position].name)
//        holder.singer.setText(list[position].singer)
//        holder.duration.setText(list[position].duration)
//        holder.statTime.setText("00:00")
//
//        holder.seekBar.visibility = View.GONE
//        holder.startOrPause.visibility = View.GONE
//        holder.statTime.visibility = View.GONE
//        holder.duration.visibility = View.GONE
//
//
//
//        list[position].isCheck=true
//        holder.itemView.setOnClickListener {
//            if (list[position].isCheck){
//                list[position].isCheck=false
//                startMusic(holder,position)
//                mediaPlayer.start()
//            }else{
//                mediaPlayer.pause()
//                holder.startOrPause.load(R.mipmap.puas)
//                list[position].isCheck=true
//            }
//            for ((index,result)in list.withIndex()){
//                if (position!=index){
//                    list[index].isCheck=false
//                    notifyItemChanged(index)
//                }
//            }
//        }
//
//
//
//    }
//
//    fun startMusic(holder: ViewHodel,position: Int){
//        holder.seekBar.visibility=View.VISIBLE
//        holder.startOrPause.visibility = View.VISIBLE
//        holder.statTime.visibility = View.VISIBLE
//        holder.duration.visibility = View.VISIBLE
//
//        mediaPlayer.setDataSource(list[position].musicLinks)
//        mediaPlayer.prepare()
//
//        holder.startOrPause.setOnClickListener {
//            if(mediaPlayer.isPlaying){
//                holder.startOrPause.load(R.mipmap.puas)
//                mediaPlayer.pause()
//
//            }else{
//                holder.startOrPause.load(R.mipmap.play)
//                mediaPlayer.start()
//            }
//        }
//
//        var format= SimpleDateFormat("mm:ss")
//        val split=list[position].duration.split(":")
//        handler.postDelayed(object :Runnable{
//            override fun run() {
//                var currentPosition= mediaPlayer.currentPosition
//                holder.seekBar.max=(split[0].toInt()*60+split[1].toInt())*1000
//                holder.seekBar.setProgress(currentPosition)
//                var current=format.format(currentPosition)
//                holder.statTime.setText(current)
//                Handler().postDelayed(this,100)
//            }
//        },100)
//
//        holder.seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
//            override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
//                if (p2){
//                    mediaPlayer.seekTo(p1)
//                }
//            }
//
//            override fun onStartTrackingTouch(p0: SeekBar?) {
//
//            }
//
//            override fun onStopTrackingTouch(p0: SeekBar?) {
//
//            }
//
//        })
//
//    }